package io.github.rascaler.dcenter.config;

import com.github.xiaoymin.knife4j.spring.util.RequestHandlerSelectorUtils;
import io.github.rascaler.dcenter.properties.CambrianSwaggerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @ClassName SwaggerConfig
 * @Description TODO
 * @Author wurenqing
 * @Date 2022/7/1 14:51
 */
@EnableSwagger2WebMvc
// 不加这个无法初始化properties
//@EnableConfigurationProperties(CambrianSwaggerProperties.class)
@Configuration
public class CambrianSwaggerConfig {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2(CambrianSwaggerProperties cambrianSwaggerProperties) {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title(cambrianSwaggerProperties.getTitle());
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoBuilder.build())
                // 分组名称(千万不要写，不然gateway做聚合时无效)
                // .groupName("vaccine-basic1.0版本")r
                .select()
                /*
                这里指定Controller扫描包路径
                (1)扫描多个包的玩法 RequestHandlerSelectorUtils.baseMultipartPackage()
                (2)扫描单个包 RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())
                多个包时，用逗号分割
                */
                .apis(RequestHandlerSelectorUtils.baseMultipartPackage(cambrianSwaggerProperties.getApiPackages()))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}
